const organization = this.pageObj.getApiWidgetByAliasName('org_tree');
const three = this.pageObj.getApiWidgetByAliasName('三维组件');
const search_input = this.pageObj.getApiWidgetByAliasName('search_input');
const type_selection = this.pageObj.getApiWidgetByAliasName('type_selection');
const prefix = this.pageObj.getVariables().find(v => v.name === 'environmentType')?.value || '';
const artemis = three.widgetObj?.artemis;
const $select = $(type_selection.__box);
const type = $select.find('input[type="radio"]:checked').val();

console.log(type_selection, type);
function basePost(url, body) {
  const options = {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(body),
  };
  return fetch(url, options).then(res => res.json())
}

function breakDataSource(item) {
  if (item.personId) {
    item.title = item.accountName || "默认name";
    item.userId = item.account;
    item.treeId = item.id;
  } else if(item.orgId || item.orgName) {
    item.title = item.orgName;
    item.id = item.orgId;
    item.treeId = item.orgId;
  } else if (item.deptId || item.deptName) {
    item.title = item.deptName;
    item.id = item.deptId;
    item.treeId = item.deptId;
  }

  item.type = '1'; // 叶子节点标识
  if (item.values) {
    item.children = item.values.map(breakDataSource);
    item.type = '0'
  }
  if (item.child) {
    item.children = item.child.map(breakDataSource);
    item.type = '0'
  }

  return item
}


const accountName = $(search_input.__box).find('.input-filter').val() || '' ;
basePost(`/${prefix}Communication/classroom/selectAllStructure`, { accountName }).then(({ result, success }) => {
  if (success === true) {
    const dataSource = result.map(breakDataSource);

    if (type === 'online' && artemis) {
      organization.widgetObj.option_data = artemis.filterUsersOnline(dataSource);
    } else if (type === 'offline' && artemis) {
      organization.widgetObj.option_data = artemis.filterUsersOnline(dataSource, true);
    } else {
      organization.widgetObj.option_data = dataSource;
    }

    organization.widgetObj.render();
    console.log(dataSource)
  }
});